package codeRandomThoughts.Test738单调递增的数字;

public class Solution {
    public int monotoneIncreasingDigits(int n) {
        char[] num = String.valueOf(n).toCharArray();

        //从后往前遍历,如果左边数字大于右边的数字,我们让左边数字减一,右边数字置为9
        int start = num.length;
        for (int i = num.length - 1; i >= 1; i--) {
            if (num[i - 1] > num[i]) {
                //左边数字大于右边数字
                num[i - 1]--;
                start = i;
            }
        }

        //统一把start往后的数字都变成9
        for (int i = start; i < num.length; i++) {
            num[i] = '9';
        }

        return Integer.parseInt(String.valueOf(num));
    }

}
